﻿using AdminModels;
using CoinModels;
using CoinModels.BTCe;
using CoinModels.Cryptsy;
using DataPool.Tickers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoinControllers
{
    public class CacheController
    {
        public List<PoolCoin> PoolCoins { get; set; }
        public List<CryptsyCoin> CryptsyCoins { get; set; }
        public List<BTCeCoinBase> BTCeCoins { get; set; }
        public DateTime? UpdatedAt { get; set; }

        public void UpdateCache(bool manualUpdate = false)
        {
            ResetCache();

            if (manualUpdate)
            {
                ManualUpdate();
            }
            else
            {
                if (CoinDataPooler.PoolList == null || CoinDataPooler.PoolList.Count == 0)
                {
                    ManualUpdate();
                }
                else
                {
                    PoolCoins = CoinDataPooler.PoolList;
                    CryptsyCoins = CoinDataPooler.CryptsyList;
                    BTCeCoins = CoinDataPooler.BTCeList;
                    UpdatedAt = DateTime.Now;
                }
            }
        }

        public void ResetCache()
        {
            PoolCoins = null;
            CryptsyCoins = null;
            BTCeCoins = null;
            UpdatedAt = null;
        }

        private void ManualUpdate()
        {
            PoolCoins = CoinDataPooler.GetMostRecentPoolCoins();
            CryptsyCoins = CryptsyDataCollector.GetMostRecentCryptsyCoins();
            BTCeCoins = BTCeDataCollector.GetMostRecentBTCeCoins();
            UpdatedAt = DateTime.Now;
        }
    }
}
